package three;
import java.util.*;
public class A057 {

    //39.组合总和
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> ret = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        //Arrays.sort(candidates);


        dfs(candidates,target,0,0);
        return ret;
    }

    public void dfs(int[] nums,int target,int pos,int key) { //每次从pos位置开始选择元素
        if(key >= target) {
            if(key==target) {
                ret.add(new ArrayList<>(path));
            }
            return;
        }
        for(int i=pos;i<nums.length;i++) {
            path.add(nums[i]);
            key += nums[i];
            dfs(nums,target,i,key);
            //回溯
            path.remove(path.size()-1);
            key -= nums[i];
        }

    }
}
